{extend name="common:base"/}
{block name="body"}
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-12">
        <h2>系统</h2>
    </div>
</div>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>数据库备份</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div> 
                </div>
                <div class="ibox-content">
                    <div class="row">
                        <div class="col-sm-9 m-b-xs">
                            <button class="btn btn-outline btn-primary" id="export">立即备份</button>
                            <button id="optimize" class="btn btn-outline btn-info " url="{:\\think\\Url::build('optimize')}" >优化表</button>
                            <button id="repair" class="btn btn-outline btn-warning" url="{:\\think\\Url::build('repair')}" >修复表</button>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <form id="export-form" method="post" action="{:\\think\\Url::build('export')}">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th ><input class="i-checks checkbox check-all" checked="chedked" type="checkbox"></th>
                                        <th>表名</th>
                                        <th>数据量</th>
                                        <th>数据大小</th>
                                        <th>创建时间</th>
                                        <th>备份状态</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {notempty name="data"}
                                    {volist name="data" id="vo"}
                                    <tr>
                                        <td><input class="ids i-checks" checked="chedked" type="checkbox" name="ids[]" value="{$vo.name}"></td>
                                        <td>{$vo.name}</td>
                                        <td>{$vo.rows}</td>
                                        <td>{$vo.data_length|format_bytes}</td>
                                        <td>{$vo.create_time}</td>
                                        <td class="info">未备份</td>
                                        <td>
                                            <a title="优化表" class="no-refresh ajax-get" href="{:\\think\\Url::build('optimize',['ids'=>$vo['name']])}">优化表</a>
                                            <a class=" ajax-get no-refresh" title="修复表" href="{:\\think\\Url::build('repair',['ids'=>$vo['name']])}">修复表</a>
                                        </td>
                                    </tr>
                                    {/volist}
                                    {else /}
                                <td colspan="7" class="text-center"> aOh! 暂时还没有内容! </td>
                                {/notempty}
                                </tbody>
                            </table>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="css"}<link href="__CSS__/plugins/iCheck/custom.css" rel="stylesheet">
{/block}
{block name="script"}
<script type="text/javascript" src="__JS__/plugins/iCheck/icheck.min.js"></script>
<script type="text/javascript">
    $(function () {
        highlight_subnav("{:\\think\\Url::build('DataBase/index')}");
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green'
        });
        (function ($) {
            var $form = $("#export-form"), $export = $("#export"), tables, $optimize = $("#optimize"), $repair = $("#repair");
            $optimize.add($repair).click(function () {
                $.post($(this).attr('url'), $form.serialize(), function (data) {
                    if (data.code) {
                        alert_msg(data.msg, 1);
                    } else {
                        alert_msg(data.msg, 0);
                    }
                });
                return false;
            });

            $export.click(function () {
                $export.parent().children().prop('disabled', true);
                $export.html("正在发送备份请求...");
                $.post(
                        $form.attr("action"),
                        $form.serialize(),
                        function (data) {
                            if (data.code) {
                                tables = data.data.tables;
                                $export.html(data.msg + "开始备份，请不要关闭本页面！");
                                backup(data.data.tab);
                                window.onbeforeunload = function () {
                                    return "正在备份数据库，请不要关闭！";
                                };
                            } else {
                                alert_msg(data.msg, 0);
                                $export.html("立即备份");
                                setTimeout(function () {
                                    $export.parent().children().prop('disabled', false);
                                }, 1500);
                            }
                        });
                return false;
            });

            function backup(tab, status) {
                status && showmsg(tab.id, "开始备份...(0%)");
                $.get($form.attr("action"), tab, function (data) {
                    if (data.code) {
                        showmsg(tab.id, data.msg);
                        if (!$.isPlainObject(data.data.tab)) {
                            $export.parent().children().prop('disabled', false);
                            $export.html("备份完成，点击重新备份");
                            window.onbeforeunload = function () {
                                return null;
                            };
                            return;
                        }
                        backup(data.data.tab, tab.id != data.data.tab.id);
                    } else {
                        alert_msg(data.msg, 0);
                        $export.html("立即备份");
                        setTimeout(function () {
                            $export.parent().children().prop('disabled', false);
                        }, 1500);
                    }
                });
            }

            function showmsg(id, msg) {
                $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
            }
        })(jQuery);
    });

</script>
{/block}